package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfDocumentExportInfo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.service.BpmUser;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.constant.Constants;
import com.yuandian.management.mapper.ConfDocumentExportInfoMapper;
import com.yuandian.management.service.ConfDocumentExportInfoService;
import com.yuandian.po.DocumentExportApplicationForm;
import com.yuandian.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * -Service实现
 * @author zfsn
 * @date 2023-5-11
 */
@Service
public class ConfDocumentExportInfoServiceImpl extends ServiceImpl<ConfDocumentExportInfoMapper, ConfDocumentExportInfo>
		implements ConfDocumentExportInfoService {

	@Autowired
	private ConfDocumentExportInfoMapper confDocumentExportInfoMapper;
	/**
	 * 1、提交文档导出申请
	 *
	 * @param documentExportApplicationForm
	 * @return
	 */
	@Override
	public R submitDocumentExportApplication(DocumentExportApplicationForm documentExportApplicationForm) {
		// 获取当前用户
		BpmUser user = SecurityUtils.getUser();
		if(user != null){
			Long userId = user.getId();
			String userName = user.getName();

			ConfDocumentExportInfo documentHandleInfo = new ConfDocumentExportInfo();
			documentHandleInfo.setSubmitterId(userId.intValue());
			documentHandleInfo.setSubmitterName(userName);
			documentHandleInfo.setServiceName(documentExportApplicationForm.getServiceName());
			documentHandleInfo.setModuleName(documentExportApplicationForm.getModuleName());
			documentHandleInfo.setExportBtnName(documentExportApplicationForm.getExportButtomName());
			documentHandleInfo.setDocExportParameterJson(documentExportApplicationForm.getDocExportParameterJson());
			documentHandleInfo.setSubmitTime(LocalDateTime.now());
			documentHandleInfo.setDocHandleType(documentExportApplicationForm.getDocHandleType());
			documentHandleInfo.setServiceCode(documentExportApplicationForm.getServiceCode());

			// 格式化文件名称  要是文件格式是 fileNamw_timestamp.doc
			String exportFileName = documentExportApplicationForm.getExportFileName();
			int nano = LocalDateTime.now().getNano();
			if(StringUtils.isNotEmpty(exportFileName) && exportFileName.contains(".")){
				int fileNameIndex = exportFileName.lastIndexOf(".");
				exportFileName = exportFileName.substring(0, fileNameIndex) + Constants.SEPARATOR + nano +exportFileName.substring(fileNameIndex, exportFileName.length());
			}else{
				exportFileName = exportFileName + Constants.SEPARATOR + nano;
			}
			documentHandleInfo.setExportFileName(exportFileName);

			super.save(documentHandleInfo);
			return R.ok("文档下载添加成功");
		}
		return R.failed();
	}

	/**
	 * 2、查看文档导出申请，服务端处理进程,支持分页查询
	 *
	 * @param confDocumentExportInfo
	 * @return
	 */
	@Override
	public R queryDocumentExportApplications(ConfDocumentExportInfo confDocumentExportInfo) {
		// 默认排序
		if (StringUtils.isEmpty(confDocumentExportInfo.getIsAsc()) && StringUtils.isEmpty(confDocumentExportInfo.getOrderByColumn())){
			confDocumentExportInfo.setIsAsc("desc");
			confDocumentExportInfo.setOrderByColumn("submit_time");
		}

		BpmUser user = SecurityUtils.getUser();
		Long submitterId = user.getId();
		if(submitterId == null){ //  默认查询当前登录用户，提交的文档导出记录
			Long userId = user.getId();
			confDocumentExportInfo.setSubmitterId(userId.intValue());
		}else{
			if(submitterId < 0){ //  默认查询所有用户，提交的文档导出记录

			}
		}

		Page<ConfDocumentExportInfo> page = null;
		if (confDocumentExportInfo.getPageNum() > 0 && confDocumentExportInfo.getPageSize() > 0) {
			page = new Page<>(confDocumentExportInfo.getPageNum(), confDocumentExportInfo.getPageSize());
		}
		return R.ok(confDocumentExportInfoMapper.queryDocumentExportApplications(page, confDocumentExportInfo));

	}

	/**
	 * 3、上报文件下载状态
	 *
	 * @param confDocumentExportInfo
	 * @return
	 */
	@Override
	public R uploadDocumentExportInfo(ConfDocumentExportInfo confDocumentExportInfo) {
		LambdaUpdateWrapper<ConfDocumentExportInfo> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(ConfDocumentExportInfo::getId, confDocumentExportInfo.getId());
		updateWrapper.set(ConfDocumentExportInfo::getDocHandleState, confDocumentExportInfo.getDocHandleState());
		updateWrapper.set(ConfDocumentExportInfo::getDocHandleError, confDocumentExportInfo.getDocHandleError());
		updateWrapper.set(ConfDocumentExportInfo::getDocHandleStartTime, LocalDateTime.now());
		super.update(updateWrapper);
		return R.ok();
	}
}
